package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.baiyang.server.model.system.Menu;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.model.system.JurisdictionInterface;
import com.baiyang.server.service.system.JurisdictionInterfaceService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/jurisdictionInterfaceOperation")
@Jurisdiction(value = "jurisdictionInterfaceOperation")
public class JurisdictionInterfaceCon {
	@Autowired
	private JurisdictionInterfaceService jurisdictionInterfaceService = null;
	
	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "getPage")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<JurisdictionInterface> getPage(@RequestBody JSONObject object){
		Letter<JurisdictionInterface> letter = new Letter<JurisdictionInterface>();
		try {
			Long start = System.currentTimeMillis();
			PageEntity<JurisdictionInterface> result = jurisdictionInterfaceService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail(result.getResult());
			letter.setResult("SUCCESS");
			letter.setDetail("数据获取成功！");
			Long end = System.currentTimeMillis();
			System.out.println(end - start);
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 根据条件获取一个用户信息
	 * @param jurisdictionInterface
	 * @return
	 */
	@Jurisdiction(value = "selectOne")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<JurisdictionInterface> selectOne(@RequestBody JurisdictionInterface jurisdictionInterface){
		Letter<JurisdictionInterface> letter = new Letter<JurisdictionInterface>();
		try {
			JurisdictionInterface obj = jurisdictionInterfaceService.selectOne(jurisdictionInterface);
			letter.setData(obj);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	@Jurisdiction(value = "getTree")
	@RequestMapping(value = "/getTree",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<JSONObject> getTree(){
		Letter<JSONObject> letter = new Letter<JSONObject>();
		try {
			JurisdictionInterface jurisdictionInterface = new JurisdictionInterface();
			JSONObject tree = jurisdictionInterfaceService.getTree();
			letter.setData(tree);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 插入数据
	 * @param jurisdictionInterface
	 * @return
	 */
	@Jurisdiction(value = "insert")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody JurisdictionInterface jurisdictionInterface){
		Letter<String> letter = new Letter<String>();
		Map<String, String> sub = SubjectNotNull.subject(jurisdictionInterface);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！");
			return letter;
		}
		try {
			jurisdictionInterfaceService.insert(jurisdictionInterface);
			letter.setDetail("数据新增成功");
			letter.setResult("SUCCESS！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 批量插入数据
	 * @param jurisdictionInterfaces
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<JurisdictionInterface> jurisdictionInterfaces){
		Letter<String> letter = new Letter<String>();
		try {
			jurisdictionInterfaceService.insertBatch(jurisdictionInterfaces,letter);
			letter.setDetail("数据新增成功");
			letter.setResult("SUCCESS！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件进行删除
	 * @param jurisdictionInterface
	 * @return
	 */
	@Jurisdiction(value = "delete")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody JurisdictionInterface jurisdictionInterface){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(jurisdictionInterface)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			jurisdictionInterfaceService.delete(jurisdictionInterface);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 *
	 * @param jurisdictionInterfaces
	 * @return
	 */
	@Jurisdiction(value = "deletes")
	@RequestMapping(value = "/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> deletes(@RequestBody List<JurisdictionInterface> jurisdictionInterfaces) {
		Letter<String> letter = new Letter<String>();
		if (DataTool.isBlank(jurisdictionInterfaces)) {
			letter.setDetail("请选中至少一行！");
			letter.setResult("FAIL");
			return letter;
		}
		try {
			jurisdictionInterfaceService.deletes(jurisdictionInterfaces);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝id更新
	 * @param jurisdictionInterface
	 * @return
	 */
	@Jurisdiction(value = "updateById")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody JurisdictionInterface jurisdictionInterface){
		Letter<String> letter = new Letter<String>();
		if(jurisdictionInterface.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			jurisdictionInterfaceService.updateById(jurisdictionInterface);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;	
	}
}
